// assitLabel.cpp : implementation file
//

#include "stdafx.h"
#include "assit.h"
#include "assitLabel.h"
#include "AssitTreeNode.h"


// assitLabel

IMPLEMENT_DYNAMIC(CwaiLabel, CStatic)

CwaiLabel::CwaiLabel()
{
	m_pFont = NULL;
}

CwaiLabel::~CwaiLabel()
{
	delete m_pFont;
}


BEGIN_MESSAGE_MAP(CwaiLabel, CStatic)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_CTLCOLOR_REFLECT()
	ON_WM_PAINT()
	ON_WM_CREATE()
END_MESSAGE_MAP()



// assitLabel message handlers

void CwaiLabel::AutoSize(CString& strWndText)
{
    if (!::IsWindow(GetSafeHwnd())) 
        return;
 
    // Get the current window position
    CRect rect;
    GetWindowRect(rect);
 
    CWnd* pParent = GetParent();
    if (pParent)
        pParent->ScreenToClient(rect);
  
    CDC* pDC = GetDC();
    CFont* pOldFont = pDC->SelectObject(m_pFont);
    CSize Extent;
	if(strWndText.IsEmpty())
		Extent = CSize(10,rect.Height());
	else
	{
		Extent = pDC->GetTextExtent(strWndText);

		if(GetStyle() && SS_LEFT)
		{
			Extent.cx += 10;
			Extent.cy += 4;
		}
	}
    pDC->SelectObject(pOldFont);
    ReleaseDC(pDC);
 

    // Get the text justification via the window style
    DWORD dwStyle = GetStyle();
 
    // Recalc the window size and position based on the text justification
    if (dwStyle & SS_CENTERIMAGE)
        rect.DeflateRect(0, (rect.Height() - Extent.cy)/2);
    else
        rect.bottom = rect.top + Extent.cy;
 
    if (dwStyle & SS_CENTER)   
        rect.DeflateRect((rect.Width() - Extent.cx)/2, 0);
    else if (dwStyle & SS_RIGHT) 
        rect.left  = rect.right - Extent.cx;
    else // SS_LEFT = 0, so we can't test for it explicitly 
        rect.right = rect.left + Extent.cx;
 
    // Move the window
    SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER);
}


void CwaiLabel::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	CWaiTreeNode* pTreeNode = (CWaiTreeNode*)GetParent();
	//XMLNode* xmlNode = pTreeNode->getTreeNode();
	pTreeNode->foldOrUnfold();

	CStatic::OnLButtonDown(nFlags, point);
}


void CwaiLabel::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	CStatic::OnLButtonUp(nFlags, point);
}


HBRUSH CwaiLabel::CtlColor(CDC* pDC, UINT nCtlColor)
{
	// TODO:  Change any attributes of the DC here
	HBRUSH hbr =  (HBRUSH) GetStockObject (WHITE_BRUSH) ;
	// TODO:  Return a non-NULL brush if the parent's handler should not be called
	return hbr;
}


void CwaiLabel::OnPaint()
{
	//CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CStatic::OnPaint() for painting messages
	//CRect rect;
	//GetClientRect(rect);
	
	//int left = rect.right - 10;
	//int right = rect.right;
	//int top = rect.top;
	//int bottom = rect.bottom;

	//dc.MoveTo(left,top+rect.Height()/2);
	//dc.LineTo(right, top+rect.Height()/2);

	//dc.MoveTo(left+5, top);
	//dc.LineTo(left+5, bottom);
	CStatic::OnPaint();
}


int CwaiLabel::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CStatic::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	LOGFONT lf;
	memset(&lf,0,sizeof(LOGFONT));
	wcscpy_s(lf.lfFaceName, _countof(lf.lfFaceName), L"Segoe UI");
	m_pFont = new CFont;
	if(!m_pFont->CreateFontIndirect(&lf))
	{
	return 0;
	}
	SetFont(m_pFont);

	return 0;
}
